<?php

namespace App\Http\Resources\Article;

use App\Http\Resources\BaseResource;
use App\Models\Article\ArticleModel;
use Illuminate\Http\Resources\MissingValue;

class ArticleCategoryListResource extends BaseResource
{
    public function toArray($request): array
    {
        return [
            "id"                => $this->id,
            "name"              => $this->name,
            "description"       => $this->description,
            "cover"             => $this->cover,
            "type"              => $this->type,
            "createdAt"         => formatDate($this->createdAt),
            "updatedAt"         => formatDate($this->updatedAt),
            "sort"              => $this->sort,
            "supportNum"        => $this->supportNum,
            "collectNum"        => $this->collectNum,
            "forwardNum"        => $this->forwardNum,
            "readNum"           => $this->readNum,
            "showNum"           => $this->showNum,
            "articleCount"      => $this->when($this->resource->hasAttribute("articleCount"), function () {
                return $this->articleCount;
            }, new MissingValue()),
            "createdByUsername" => $this->whenLoaded(
                "createdByUidRel",
                function () {
                    return vsprintf(
                        "%s(%s)",
                        [
                            attributeGet($this, "createdByUidRel.name", "-"),
                            attributeGet($this, "createdByUidRel.nickname", "-")
                        ]
                    );
                },
                new MissingValue()
            ),
            "updatedByUsername" => $this->whenLoaded(
                "updatedByUidRel",
                function () {
                    return vsprintf(
                        "%s(%s)",
                        [
                            attributeGet($this, "updatedByUidRel.name", "-"),
                            attributeGet($this, "updatedByUidRel.nickname", "-")
                        ]
                    );
                },
                new MissingValue()
            ),
            "putTop"            => $this->putTop
        ];
    }
}
